//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "InflationCoupon.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Indexes/InflationIndex.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Cashflows/InflationCouponPricer.h>
#include <gen/QL/Cashflows/Coupon.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CInflationCoupon::CInflationCoupon (boost::shared_ptr<QuantLib::InflationCoupon>& childNative, Object^ owner) : CCoupon(CInflationCoupon::typeid)
{
#ifdef HANDLE
	_phInflationCoupon = NULL;
#endif
	_ppInflationCoupon = &childNative;
    _ppCoupon = new boost::shared_ptr<QuantLib::Coupon> (boost::dynamic_pointer_cast<QuantLib::Coupon> (*_ppInflationCoupon));
}
Cephei::QL::Cashflows::CInflationCoupon::CInflationCoupon (QuantLib::InflationCoupon& childNative, Object^ owner) : CCoupon(CInflationCoupon::typeid)
{
#ifdef HANDLE
	_phInflationCoupon = NULL;
#endif
	_ppInflationCoupon = new boost::shared_ptr<QuantLib::InflationCoupon> (&childNative);
    _ppCoupon = new boost::shared_ptr<QuantLib::Coupon> (boost::dynamic_pointer_cast<QuantLib::Coupon> (*_ppInflationCoupon));
    _InflationCouponOwner = owner;
    _CouponOwner = owner;
}

Cephei::QL::Cashflows::CInflationCoupon::CInflationCoupon (CInflationCoupon^ copy) : CCoupon(CInflationCoupon::typeid)
{
#ifdef HANDLE
	_phInflationCoupon = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppInflationCoupon = new boost::shared_ptr<QuantLib::InflationCoupon> (copy->GetShared());
        _ppCoupon = new boost::shared_ptr<QuantLib::Coupon> (boost::dynamic_pointer_cast<QuantLib::Coupon> (*_ppInflationCoupon));
    }
}
Cephei::QL::Cashflows::CInflationCoupon::CInflationCoupon (System::Type^ t) : CCoupon(CInflationCoupon::typeid)
{
#ifdef HANDLE
	_phInflationCoupon = NULL;
#endif
	if (!t->IsSubclassOf(CInflationCoupon::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CInflationCoupon::CInflationCoupon (QuantLib::Handle<QuantLib::InflationCoupon>& childNative, Object^ owner)  : CCoupon(CInflationCoupon::typeid)
{
	_phInflationCoupon = &childNative;
	_ppInflationCoupon = &static_cast<boost::shared_ptr<QuantLib::InflationCoupon>>(childNative.currentLink());
    _ppCoupon = new boost::shared_ptr<QuantLib::Coupon> (boost::dynamic_pointer_cast<QuantLib::Coupon> (*_ppInflationCoupon));
    _InflationCouponOwner = owner;
}
Cephei::QL::Cashflows::CInflationCoupon::CInflationCoupon (QuantLib::Handle<QuantLib::InflationCoupon> childNative)  : CCoupon(CInflationCoupon::typeid)
{
	_phInflationCoupon = &childNative;
	_ppInflationCoupon = &static_cast<boost::shared_ptr<QuantLib::InflationCoupon>>(childNative.currentLink());
    _ppCoupon = new boost::shared_ptr<QuantLib::Coupon> (boost::dynamic_pointer_cast<QuantLib::Coupon> (*_ppInflationCoupon));
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CInflationCoupon::CInflationCoupon (QuantLib::InflationCoupon childNative)  : CCoupon(CInflationCoupon::typeid)
{
#ifdef HANDLE
	_phInflationCoupon = NULL;
#endif
	_ppInflationCoupon = new boost::shared_ptr<QuantLib::InflationCoupon> (new QuantLib::InflationCoupon (childNative));
    _ppCoupon = new boost::shared_ptr<QuantLib::Coupon> (boost::dynamic_pointer_cast<QuantLib::Coupon> (*_ppInflationCoupon));
}
#endif

Cephei::QL::Cashflows::CInflationCoupon::~CInflationCoupon ()
{
    if (_ppInflationCoupon != NULL)
    {
	    delete _ppInflationCoupon;
        _ppInflationCoupon = NULL;
    }
}
Cephei::QL::Cashflows::CInflationCoupon::!CInflationCoupon ()
{
    if (_ppInflationCoupon != NULL)
    {
	    delete _ppInflationCoupon;
    }
}
QuantLib::InflationCoupon& Cephei::QL::Cashflows::CInflationCoupon::GetReference ()
{
    if (_ppInflationCoupon == NULL) throw gcnew NativeNullException ();
	return **_ppInflationCoupon;
}
boost::shared_ptr<QuantLib::InflationCoupon>& Cephei::QL::Cashflows::CInflationCoupon::GetShared ()
{
    if (_ppInflationCoupon == NULL) throw gcnew NativeNullException ();
	return *_ppInflationCoupon;
}
QuantLib::InflationCoupon* Cephei::QL::Cashflows::CInflationCoupon::GetPointer ()
{
    if (_ppInflationCoupon == NULL) throw gcnew NativeNullException ();
	return &**_ppInflationCoupon;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::InflationCoupon>& Cephei::QL::Cashflows::CInflationCoupon::GetHandle ()
{
	if (_phInflationCoupon == NULL)
	{
		_phInflationCoupon = new Handle<QuantLib::InflationCoupon> (*_ppInflationCoupon);
	}
	return *_phInflationCoupon;
}
#endif
bool Cephei::QL::Cashflows::CInflationCoupon::HasNative () 
{
	return (_ppInflationCoupon != NULL);
}

Double Cephei::QL::Cashflows::CInflationCoupon::AccruedAmount (DateTime d)
{
    try
    {
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppInflationCoupon)->accruedAmount ( _d );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CInflationCoupon::Amount::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppInflationCoupon)->amount ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IDayCounter^ Cephei::QL::Cashflows::CInflationCoupon::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter _rv = (QuantLib::DayCounter)(*_ppInflationCoupon)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = gcnew Cephei::QL::Times::CDayCounter (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Cashflows::CInflationCoupon::FixingDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppInflationCoupon)->fixingDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt32 Cephei::QL::Cashflows::CInflationCoupon::FixingDays::get ()
{
    try
    {
    	QuantLib::Natural _rv = (QuantLib::Natural)(*_ppInflationCoupon)->fixingDays ( );   
        UInt32 _nrv = (UInt32)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Indexes::IInflationIndex^ Cephei::QL::Cashflows::CInflationCoupon::Index::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::InflationIndex>& _rv = (boost::shared_ptr<QuantLib::InflationIndex>&)(*_ppInflationCoupon)->index ( );   
        Cephei::QL::Indexes::CInflationIndex^ _nrv = gcnew Cephei::QL::Indexes::CInflationIndex (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CInflationCoupon::IndexFixing::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppInflationCoupon)->indexFixing ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IPeriod^ Cephei::QL::Cashflows::CInflationCoupon::ObservationLag::get ()
{
    try
    {
    	QuantLib::Period _rv = (QuantLib::Period)(*_ppInflationCoupon)->observationLag ( );   
        Cephei::QL::Times::CPeriod^ _nrv = gcnew Cephei::QL::Times::CPeriod (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CInflationCoupon::Price (Cephei::QL::Termstructures::IYieldTermStructure^ discountingCurve)
{
    CYieldTermStructure^ _CdiscountingCurve;
    try
    {
        _CdiscountingCurve = safe_cast<CYieldTermStructure^> (discountingCurve);
        _CdiscountingCurve->Lock();
        Handle<QuantLib::YieldTermStructure>& _discountingCurve = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdiscountingCurve->GetHandle ()); 
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppInflationCoupon)->price ( _discountingCurve );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CdiscountingCurve != nullptr) _CdiscountingCurve->Unlock();
    }
}
Cephei::QL::Cashflows::IInflationCouponPricer^ Cephei::QL::Cashflows::CInflationCoupon::Pricer::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::InflationCouponPricer> _rv = (boost::shared_ptr<QuantLib::InflationCouponPricer>)(*_ppInflationCoupon)->pricer ( );   
        Cephei::QL::Cashflows::CInflationCouponPricer^ _nrv = gcnew Cephei::QL::Cashflows::CInflationCouponPricer (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CInflationCoupon::Rate::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppInflationCoupon)->rate ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Cashflows::IInflationCoupon^ Cephei::QL::Cashflows::CInflationCoupon::SetPricer (Cephei::QL::Cashflows::IInflationCouponPricer^ pricer)
{
    CInflationCouponPricer^ _Cpricer;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cpricer = safe_cast<CInflationCouponPricer^> (pricer);
        _Cpricer->Lock();
        boost::shared_ptr<QuantLib::InflationCouponPricer>& _pricer = static_cast<boost::shared_ptr<QuantLib::InflationCouponPricer>&> (_Cpricer->GetShared ()); 
    	(*_ppInflationCoupon)->setPricer ( _pricer );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpricer != nullptr) _Cpricer->Unlock();
    }
}
Cephei::QL::Cashflows::IInflationCoupon^ Cephei::QL::Cashflows::CInflationCoupon::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppInflationCoupon)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

